package cn.tedu.eg_user.base.exception;

import cn.tedu.eg_user.base.response.JsonResult;
import cn.tedu.eg_user.base.response.StatusCode;
import jakarta.validation.ConstraintViolationException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(MethodArgumentNotValidException.class)
    public JsonResult doHandlerMethodArgumentNotValidException(MethodArgumentNotValidException ex){
      String date=ex.getFieldError().getDefaultMessage();
      log.error("MethodArgumentNotValidException:"+date);
      return new JsonResult(StatusCode.VALIDATED_ERROR,date);
    }

    @ExceptionHandler(ConstraintViolationException.class)
    public JsonResult doHandlerConstraintViolationException(ConstraintViolationException ex){
        String date=ex.getMessage().split(":")[1].trim();
        log.error("ConstraintViolationException:"+date);
        return new JsonResult(StatusCode.VALIDATED_ERROR,date);
    }


}
